package com.b2.controller.wx;

import com.b2.constant.JwtClaimsConstant;
import com.b2.pojo.dto.UserLoginDTO;
import com.b2.pojo.entity.User;
import com.b2.pojo.vo.UserLoginVO;
import com.b2.properties.JwtProperties;
import com.b2.result.Result;
import com.b2.service.UserService;
import com.b2.utils.JwtUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wx/user")
@Slf4j
@Api(tags = "微信用户相关接口")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
     //用户登录
    @PostMapping("login")
    public Result<UserLoginVO> login(@RequestBody   UserLoginDTO userLoginDTO){
        User user= userService.login(userLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.User_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        UserLoginVO build = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .phone(user.getPhone())
                .build();
        return Result.success(build);
    }

    //用户第一次登录用于添加电话号码
    @GetMapping("addphone")
    public Result addphone( String phone){
         userService.addphone(phone);
         return Result.success();
    }
   //用于登陆后进行身份识别（0为园长，1为老师）
    @GetMapping("identificate")
    public Result identificate(String id ){
       Result result =userService.identificate(id);
        return  result;
    }
}
